package com.shrct.businessprocess.budget.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.budget.RCBudget;
import com.shrct.businessprocess.budget.RCBudgetCategory;
import com.shrct.businessprocess.budget.RCCost4RemainItem;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.budget.RCF1Budget;
import com.shrct.businessprocess.budget.impl.RCBudgetImpl;
import com.shrct.businessprocess.budget.proxy.RCF1BudgetProxy;
import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCF1BUDGET_TYPE")
public class RCF1BudgetImpl extends RCBudgetImpl implements RCF1Budget {
  public RCF1BudgetImpl() {
    super();
  }
  
  public RCF1BudgetImpl(final RCCostBreakDown cbd) {
    super( cbd);
    
  }
  
  public RCProjectMoney getContractPrice() {
    RCProjectMoney _rCProjectMoney = new RCProjectMoney();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          it.setAmount(Double.valueOf(0d));
          RCCostBreakDown _cbd = RCF1BudgetImpl.this.getCbd();
          RCExchangeRate _exchangeRate = _cbd.getExchangeRate();
          it.setExchange(_exchangeRate);
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function);
    return _doubleArrow;
  }
  
  public Double getContractTaxRate() {
    return Double.valueOf(0d);
  }
  
  public Double getNetTaxRate() {
    return Double.valueOf(0d);
  }
  
  private RCProjectMoney getCostAmount(final Function1<? super RCBudget,? extends RCProjectMoney> calc) {
    RCProjectMoney _xblockexpression = null;
    {
      RCCostBreakDown _cbd = this.getCbd();
      RCProject _project = _cbd.getProject();
      final RCExchangeRate rate = _project.getDefaultExchangeRate();
      RCCostBreakDown _cbd_1 = this.getCbd();
      Collection<RCBudget> _budgets = _cbd_1.getBudgets();
      final Function1<RCBudget,Boolean> _function = new Function1<RCBudget,Boolean>() {
          public Boolean apply(final RCBudget it) {
            RCBudgetCategory _category = it.getCategory();
            boolean _notEquals = (!Objects.equal(_category, RCBudgetCategory.PartF));
            return Boolean.valueOf(_notEquals);
          }
        };
      Iterable<RCBudget> _filter = IterableExtensions.<RCBudget>filter(_budgets, _function);
      final Function1<RCBudget,RCProjectMoney> _function_1 = new Function1<RCBudget,RCProjectMoney>() {
          public RCProjectMoney apply(final RCBudget it) {
            RCProjectMoney _apply = calc.apply(it);
            return _apply;
          }
        };
      Iterable<RCProjectMoney> _map = IterableExtensions.<RCBudget, RCProjectMoney>map(_filter, _function_1);
      final RCProjectMoney result = Moneys.sum(_map, rate);
      RCProjectMoney _xifexpression = null;
      Double _amount = result.getAmount();
      boolean _lessThan = ((_amount).doubleValue() < 0);
      if (_lessThan) {
        RCCostBreakDown _cbd_2 = this.getCbd();
        RCProject _project_1 = _cbd_2.getProject();
        RCProjectMoney _zeroAmount = _project_1.getZeroAmount();
        _xifexpression = _zeroAmount;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  public RCProjectMoney getNetPrice() {
    final Function1<RCBudget,RCProjectMoney> _function = new Function1<RCBudget,RCProjectMoney>() {
        public RCProjectMoney apply(final RCBudget it) {
          RCProjectMoney _invoiceTax = it.getInvoiceTax();
          return _invoiceTax;
        }
      };
    RCProjectMoney _costAmount = this.getCostAmount(_function);
    return _costAmount;
  }
  
  public RCProjectMoney getInvoiceTax() {
    RCCostBreakDown _cbd = this.getCbd();
    RCProject _project = _cbd.getProject();
    RCProjectMoney _zeroAmount = _project.getZeroAmount();
    return _zeroAmount;
  }
  
  public String getTypeId() {
    return "RCF1Budget";
  }
  
  public Boolean seemsFinished() {
    return Boolean.valueOf(true);
  }
  
  public Iterable<RCCost4RemainItem> getRCCost4RemainItems() {
    ArrayList<RCCost4RemainItem> _newArrayList = CollectionLiterals.<RCCost4RemainItem>newArrayList();
    return _newArrayList;
  }
  
  public static RCF1Budget create(final RCCostBreakDown cbd, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.budget.RCF1Budget rCF1Budget = new com.shrct.businessprocess.budget.impl.RCF1BudgetImpl(
    	cbd
    );
    objectFactory.create(rCF1Budget);
    return rCF1Budget;			
    
  }
  
  public static RCF1Budget createRCF1BudgetImpl(final RCCostBreakDown cbd, final IObjectFactory objectFactory, final Procedure1<RCF1Budget> init) {
    com.shrct.businessprocess.budget.RCF1Budget rCF1Budget = new com.shrct.businessprocess.budget.impl.RCF1BudgetImpl(
    	cbd
    );
    objectFactory.create(rCF1Budget);
    init.apply(rCF1Budget);
    
    return rCF1Budget;			
    
  }
  
  public RCF1BudgetProxy toProxy() {
    com.shrct.businessprocess.budget.proxy.RCF1BudgetProxy proxy = new com.shrct.businessprocess.budget.proxy.RCF1BudgetProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
